home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / fragment tool / windows.c < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  30.7 KB  |  1,379 lines

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include <Types.h>
  24. #include <Quickdraw.h>
  25. #include <QDOffscreen.h>
  26. #include <Devices.h>
  27. #include <LowMem.h> /* For MemError() */
  28. #include <Script.h>
  29. #include <TextUtils.h>
  30. #include <Dialogs.h>
  31. #include <Folders.h>
  32. #include <stddef.h>
  33. #include <Sound.h>
  34.  
  35. #include <CodeFragments.h>
  36. #include <StandardFile.h>
  37. #include <Folders.h>
  38.  
  39. #include "FragmentTool.h"
  40. #include "FragmentStuff.h"
  41. #include "Dialogs.h"
  42. #include "Streams.h"
  43. #include "Utilities.h"
  44.  
  45. #include "Prototypes.h"
  46.  
  47.  
  48. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon );
  49. static void AddContentsToList ( WindowRef theWindow );
  50. static void DoDialogItemHit ( DialogRef theDialog, int16 theItem );
  51. static int8 GetStageFromItem ( int16 theItem );
  52. static int16 GetItemFromStage ( int8 theStage );
  53.  
  54. static OSErr CreateTempAndCopyFragment ( tHeaderHan sourceHeader, FSSpecPtr sourceSpec, int16 sourceIndex,
  55.                                         tHeaderHan targetHeader );
  56.  
  57.  
  58.  
  59.  
  60. void DoNewDocument ( void )
  61. {
  62.     OSErr            theErr;
  63.     int16            theRef;
  64.     int16            theVol;
  65.     int32            theDir;
  66.     FSSpec            theSpec;
  67.     Str255            tmpStr = "\p";
  68.     Str255            theFile = "\pFragmentTool ";
  69.     hdrHand            theHeader;
  70.     Size            headerSize;
  71.     
  72.     
  73.     theErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &theVol, &theDir );
  74.     OSTypeToPStr ( TickCount ( ), tmpStr );
  75.     ConcatPStr ( theFile, tmpStr, sizeof ( Str255 ) );
  76.     theErr = FSMakeFSSpec ( theVol, theDir, theFile, &theSpec );
  77.     theErr = FSpCreate ( &theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  78.     
  79.  
  80.     // The file has been created with both forks, but it doesn't have a
  81.     // resource map yet. If we try to open it now, we'll get an eofErr (-39).
  82.     FSpCreateResFile ( &theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  83.     theRef = FSpOpenResFile ( &theSpec, fsRdWrPerm );
  84.     if ( theRef == -1 )
  85.     {
  86.         theErr = ResError ( );
  87.         AlertUser ( kGenericErrorStr, theErr, nil );
  88.         return;
  89.     }
  90.     
  91.     headerSize = offsetof ( cfrgHeader, arrayStart );
  92.     theHeader = (hdrHand) NewHandleClear ( headerSize );
  93.     (*theHeader)->version = 1;        // Current version number
  94.     AddResource ( (Handle) theHeader, kCFragResourceType, kCFragResourceID, "\p" );
  95.     CloseResFile ( theRef );
  96.     
  97.     GetIndString ( tmpStr, 1000, 4 );                // Default document name
  98.     theErr = OpenDocument ( &theSpec, tmpStr );
  99.     if ( theErr )
  100.     {
  101.         AlertUser ( kGenericErrorStr, theErr, nil );
  102.         return;
  103.     }
  104.     
  105.     return;
  106.     
  107. }
  108.  
  109.  
  110.  
  111. void DoOpenDocument ( void )
  112. {
  113.     OSErr                theErr;
  114.     StandardFileReply    theReply;
  115.     
  116.     
  117.     StandardGetFile ( nil, -1, nil, &theReply );
  118.     if ( theReply.sfGood )
  119.     {
  120.         theErr = OpenDocument ( &theReply.sfFile, nil );
  121.         
  122.         if ( theErr )
  123.             AlertUser ( kGenericErrorStr, theErr, nil );
  124.     }
  125.     
  126.     return;
  127. }
  128.  
  129.  
  130.  
  131. OSErr OpenDocument ( FSSpecPtr fileSpec, StringPtr documentName )
  132. {
  133.     OSErr                theErr;
  134.     int16                theFileRef, saveFile;
  135.     WindowRef            theWindow;
  136.     tWindowInfoPtr        theInfo;
  137.     tAddFragmentsRec    theRec;
  138.     Str255                theTitle;
  139.     
  140.     if ( documentName )
  141.         CopyPStr ( documentName, theTitle, sizeof ( Str255 ) );
  142.     else
  143.         CopyPStr ( fileSpec->name, theTitle, sizeof ( Str255 ) );
  144.     
  145.     saveFile = CurResFile ( );
  146.     SetResLoad ( false );
  147.     theFileRef = FSpOpenResFile ( fileSpec, fsRdPerm );
  148.     SetResLoad ( true );
  149.     UseResFile ( saveFile );
  150.     if ( theFileRef == -1 )
  151.         return ResError ( );
  152.     
  153.     theRec.u.out.theFileRef = theFileRef;
  154.     theErr = CreateDocumentWindow ( &theWindow, theTitle, AddFragments, (void*) &theRec );
  155.     if ( theErr )
  156.         return theErr;
  157.     
  158.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  159.     theInfo->bUntitled = (documentName) ? true : false;
  160.     theInfo->dataHandle = theRec.u.in.theDataHandle;
  161.     BlockMoveData ( fileSpec, &theInfo->fileSpec, sizeof ( FSSpec ) );
  162.     
  163.     return theErr;
  164. }
  165.  
  166.  
  167.  
  168. OSErr CreateDocumentWindow ( WindowRef* windowRef, Str255 windowTitle,
  169.                                 tContentsProcPtr contentsProc, void* refCon )
  170. {
  171.     OSErr        theErr;
  172.     WindowRef    theWindow;
  173.     
  174.     
  175.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1L );
  176.     if ( theWindow == nil )
  177.         return (ResError ( )) ? ResError ( ) : resNotFound;
  178.     
  179.     theErr = CreateWindowInfo ( theWindow, sizeof ( tWindowInfo ) );
  180.     if ( theErr )    goto CleanupAndBail;
  181.     
  182.      theErr = CreateContentList ( theWindow, contentsProc, refCon );
  183.     if ( theErr )    goto CleanupAndBail;
  184.     
  185.     
  186.     // Since the application can function without the Drag Manager, an
  187.     // error here is not considered fatal.
  188.     theErr = InstallDragHandlers ( theWindow );
  189.     
  190.     SetWindowType ( theWindow, kDocumentWindowType );
  191.     SetPortWindowPort ( theWindow );
  192.     TextFont ( kFontIDGeneva );
  193.     TextFace ( normal );
  194.     TextSize ( 9 );
  195.     
  196.     SetWTitle ( theWindow, windowTitle );
  197.     SelectWindow ( theWindow );
  198.     ShowWindow ( theWindow );
  199.     
  200.     *windowRef = theWindow;
  201.     
  202.     return noErr;
  203.     
  204.  
  205. CleanupAndBail:
  206.     
  207.     // Don't forget to free any storage we've used so far
  208.     DestroyDocumentWindow ( theWindow );
  209.     
  210.     return theErr;
  211. }
  212.  
  213.  
  214.  
  215. //
  216. // This will close the document's file, dispose of any storage we've hung
  217. // off the window, remove any drag handlers, and then dispose of the window.
  218. //
  219. WindowRef DestroyDocumentWindow ( WindowRef windowRef )
  220. {
  221.     if ( windowRef )
  222.     {
  223.         tWindowInfoPtr theInfo;
  224.         
  225.         theInfo = (tWindowInfoPtr) GetWRefCon ( windowRef );
  226.         if ( theInfo )
  227.         {
  228.             if ( theInfo->listRef )
  229.                 LDispose ( theInfo->listRef );
  230.             
  231.             if ( theInfo->dataHandle )
  232.                 DisposeHandle ( theInfo->dataHandle );
  233.  
  234.             DisposePtr ( (Ptr) theInfo );
  235.         }
  236.         
  237.         RemoveDragHandlers ( windowRef );
  238.         DisposeWindow ( windowRef );
  239.         
  240.     }
  241.     
  242.     // Return nil so it can be assigned to the window
  243.     // reference which was passed in
  244.     return nil;
  245. }
  246.  
  247.  
  248.  
  249. ////////////////////////////////////////////////////////////////////////////////////
  250.  
  251.  
  252.  
  253. //
  254. // Draw *only* the grow icon
  255. //
  256. void DrawClippedGrowIcon ( WindowRef theWindow )
  257. {
  258.     Rect        portRect = theWindow->portRect;
  259.     RgnHandle    oldClip = NULL;
  260.     Rect        newClip;
  261.     
  262.     
  263.     SetPort ( theWindow );
  264.     oldClip = NewRgn ( );
  265.     GetClip ( oldClip );
  266.     
  267.     newClip = portRect;
  268.     newClip.top = newClip.bottom - 15;
  269.     newClip.left = newClip.right - 15;
  270.     ClipRect ( &newClip );
  271.     DrawGrowIcon ( theWindow );
  272.     
  273.     SetClip ( oldClip );
  274.     DisposeRgn ( oldClip );
  275.     
  276.     return;
  277. }
  278.  
  279.  
  280.  
  281.  
  282. //
  283. // This is passed into the CreateDocumentWindow routine
  284. // to add the contents to the newly created list.
  285. //
  286. OSErr AddFragments ( ListRef theList, void* refCon )
  287. {
  288.     OSErr                theErr;
  289.     int16                theFileRef, saveFile;
  290.     int                    index, itemCount;
  291.     Handle                resourceScratch;
  292.     tHeaderHan            parsedResource;
  293.     tAddFragmentsRec*    theRec;
  294.     
  295.     
  296.     
  297.     
  298.     theRec = (tAddFragmentsRec*) refCon;
  299.     theFileRef = theRec->u.out.theFileRef;
  300.     
  301.     parsedResource = (tHeaderHan) NewHandleClear ( sizeof ( tHeader ) );
  302.     theErr = MemError ( );
  303.     if ( theErr )
  304.         return theErr;
  305.     
  306.     saveFile = CurResFile ( );
  307.     UseResFile ( theFileRef );
  308.     resourceScratch = Get1Resource ( kCFragResourceType, kCFragResourceID );
  309.     theErr = ResError ( );
  310.     if ( theErr || resourceScratch == nil)
  311.         return (theErr) ? theErr : resNotFound;
  312.     UseResFile ( saveFile );
  313.     
  314.     
  315.     if ( parsedResource )
  316.     {
  317.         ParseResource ( resourceScratch, parsedResource );
  318.         
  319.         itemCount = (*parsedResource)->itemCount;
  320.         for ( index = 0; index < itemCount; index++ )
  321.         {
  322.             tItemPtr     dstItem;
  323.             
  324.             dstItem = &(*parsedResource)->itemList[index];
  325.             AddFragToList ( theList, dstItem );
  326.             
  327.         }
  328.         
  329.         
  330.         theRec->u.in.theDataHandle = (Handle) parsedResource;
  331.     }
  332.     
  333.     ReleaseResource ( resourceScratch );
  334.         
  335.     return noErr;
  336.     
  337. } // AddFragments
  338.  
  339.  
  340.  
  341. //
  342. // This returns the Nth document window, ignoring the front-most
  343. //
  344. WindowRef GetIndexedDocumentWindow ( int theIndex )
  345. {
  346.     Boolean        bFirst = true;
  347.     int            thisIndex = 0;        // Must be zero-based, like the List Manager
  348.     WindowRef    currWindow;
  349.     
  350.     
  351.     currWindow = FrontWindow ( );
  352.     while ( currWindow )
  353.     {
  354.         if ( GetWindowType ( currWindow ) == kDocumentWindowType )
  355.         {
  356.             // Skip the front-most document since that's the source document
  357.             if ( bFirst )
  358.                 bFirst = false;
  359.             else
  360.             {
  361.                 if ( thisIndex == theIndex )
  362.                     return currWindow;
  363.                 
  364.                 thisIndex++;
  365.             }
  366.         }
  367.         
  368.         currWindow = GetNextWindow ( currWindow );
  369.     }
  370.     
  371.     return nil;
  372. }
  373.  
  374.  
  375.  
  376. //
  377. // Creates the storage for the data to hang off a window or dialog
  378. //
  379. OSErr CreateWindowInfo ( WindowRef windowRef, Size infoSize )
  380. {
  381.     OSErr    theErr;
  382.     Ptr        theInfo = nil;
  383.     
  384.     
  385.     theInfo = NewPtrClear ( infoSize );
  386.     theErr = MemError ( );
  387.     if ( theErr )
  388.         return theErr;
  389.     
  390.     SetWRefCon ( windowRef, (long) theInfo );
  391.     
  392.     return noErr;
  393. }
  394.  
  395.  
  396.  
  397. //
  398. // Closes all the open documents. Called just before a quit, or as
  399. // the result of an option click in a windows go away box.
  400. //
  401. Boolean DoCloseAllDocuments ( void )
  402. {
  403.     Boolean        bCancelled = false;
  404.     WindowRef    aWindow;
  405.     
  406.     aWindow = FrontWindow ( );
  407.     while ( aWindow && !bCancelled )
  408.     {
  409.         bCancelled = DoCloseDocument ( aWindow );
  410.         aWindow = GetNextWindow ( aWindow );
  411.     }
  412.     
  413.     return bCancelled;
  414. }
  415.  
  416.  
  417.  
  418. Boolean DoCloseDocument ( WindowRef theWindow )
  419. {
  420.     int16    windowType;
  421.     
  422.     if ( theWindow )
  423.     {
  424.         windowType = GetWindowType ( theWindow );
  425.         if ( windowType == kDAWindowType )
  426.             CloseDeskAcc ( GetWindowKind ( theWindow ) );
  427.         else if ( windowType == kDocumentWindowType )
  428.         {
  429.             int                i;
  430.             tWindowInfoPtr    theInfo;
  431.             tHeaderHan        theHeader;
  432.             tItemPtr        anItem;
  433.             
  434.             
  435.             // Check for any fragments that have temp files
  436.             theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  437.             if ( theInfo->bDirty )
  438.                 if ( DoSaveBeforeClosing ( theWindow ) ) // Did the user cancel?
  439.                     return kCancelled;
  440.                     
  441.             theHeader = (tHeaderHan) theInfo->dataHandle;
  442.             HLock ( (Handle) theHeader );
  443.             for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  444.             {
  445.                 anItem = GetNthItem ( theHeader, i );
  446.                 if ( anItem == nil )
  447.                     break;
  448.                 
  449.                 DecrementTempUsageCount ( anItem );
  450.             }
  451.             HUnlock ( (Handle) theHeader );
  452.  
  453.             DestroyDocumentWindow ( theWindow );
  454.         }    
  455.         else if ( windowType == kListWindowType )
  456.             DestroyDialog ( theWindow );
  457.             
  458.         AdjustMenus ( );
  459.     }
  460.     
  461.     return kNotCancelled;
  462.     
  463. } // CloseDocument
  464.  
  465.  
  466.  
  467. Boolean DoSaveBeforeClosing ( WindowRef theWindow )
  468. {
  469.     int16        theItem;
  470.     DialogRef    theDialog;
  471.     WindowRef    dialogWindow;
  472.     Str255        theTitle, theReason;
  473.     
  474.     
  475.     theDialog = GetNewDialog ( kSaveDontsaveDialog, nil, (WindowRef) -1 );
  476.     dialogWindow = GetDialogWindow ( theDialog );
  477.     SetPortWindowPort ( dialogWindow );
  478.     
  479.     GetWTitle ( theWindow, theTitle );
  480.     if ( gQuit )
  481.         GetIndString ( theReason, 1000, 6 );
  482.     else
  483.         GetIndString ( theReason, 1000, 7 );
  484.     ParamText ( theTitle, theReason, nil, nil );
  485.     ShowWindow ( dialogWindow );
  486.     SelectWindow ( dialogWindow );
  487.     
  488.     SetDialogDefaultItem ( theDialog, kStdOkItemIndex );
  489.     SetDialogCancelItem ( theDialog, kStdCancelItemIndex );
  490.     
  491.     
  492.     // As long as the only enabled items are the dimissers, 
  493.     // there is no need to call ModalDialog within a loop.
  494.     ModalDialog ( nil, &theItem );
  495.     DisposeDialog ( theDialog );
  496.     
  497.     if ( theItem == kStdOkItemIndex )
  498.         return DoSave ( theWindow );
  499.     
  500.     return theItem == kStdCancelItemIndex;
  501. }        
  502.  
  503.  
  504.  
  505. Boolean DoSave ( WindowRef theWindow )
  506. {
  507.     int16            saveFile, theFileRef;
  508.     int                i;
  509.     OSErr            theErr;
  510.     Handle            resourceScratch;
  511.     tWindowInfoPtr    theInfo;
  512.     tHeaderHan        theHeader;
  513.     tItemPtr        anItem;
  514.     
  515.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  516.     #if DEBUGGING
  517.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  518.     #endif
  519.     
  520.     if ( theInfo->bUntitled )
  521.         return DoSaveAs ( theWindow );
  522.     
  523.     // Check for any fragments that have been deleted...
  524.     theHeader = (tHeaderHan) theInfo->dataHandle;
  525.     HLock ( (Handle) theHeader );
  526.     for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  527.     {
  528.         anItem = GetNthItem ( theHeader, i );
  529.         if ( anItem->bDeleted )
  530.         {
  531.             if ( anItem->bExistsInDocument )
  532.             {
  533.                 theErr = DeleteFragment ( theHeader, &theInfo->fileSpec, i );
  534.                 if ( theErr )
  535.                 {
  536.                     AlertUser ( kGenericErrorStr, theErr, nil );
  537.                     return kNotCancelled;
  538.                 }
  539.                 anItem->bExistsInDocument = false;
  540.             }
  541.             
  542.             DecrementTempUsageCount ( anItem );
  543.         }
  544.     }
  545.     
  546.     // ...and for any new fragments
  547.     for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  548.     {
  549.         anItem = GetNthItem ( theHeader, i );
  550.         
  551.         if ( anItem->bDeleted || anItem->bExistsInDocument )
  552.             continue;
  553.             
  554.         anItem->codeOffset = 0L;
  555.         theErr = AppendFileData ( GetTempSpecPtr ( anItem ), &theInfo->fileSpec, &anItem->codeOffset, &anItem->codeLength );
  556.         if ( theErr )
  557.         {
  558.             AlertUser ( kGenericErrorStr, theErr, nil );
  559.             return kNotCancelled;
  560.         }
  561.         
  562.         anItem->bExistsInDocument = true;
  563.     }
  564.     
  565.     HUnlock ( (Handle) theHeader );
  566.     
  567.     
  568.     saveFile = CurResFile ( );
  569.     theFileRef = FSpOpenResFile ( &theInfo->fileSpec, fsRdWrPerm );
  570.     theErr = ResError ( );
  571.     if ( theErr )
  572.     {
  573.         int16    theIndex;
  574.         
  575.         theIndex = (theErr == opWrErr || theErr == permErr) ? kFileOpenStr : kGenericErrorStr;
  576.         AlertUser ( theIndex, (theErr == noErr) ? resNotFound : theErr, nil );
  577.         return kNotCancelled;
  578.     }
  579.     UseResFile ( theFileRef );
  580.     
  581.     resourceScratch = Get1Resource ( kCFragResourceType, kCFragResourceID );
  582.     theErr = ResError ( );
  583.     if ( theErr || resourceScratch == nil )
  584.     {
  585.         AlertUser ( kGenericErrorStr, (theErr == noErr) ? resNotFound : theErr, nil );
  586.         return kNotCancelled;
  587.     }
  588.     
  589.     theErr = BuildResource ( (tHeaderHan) theInfo->dataHandle, resourceScratch );
  590.     if ( theErr )
  591.     {
  592.         AlertUser ( kGenericErrorStr, theErr, nil );
  593.         return kNotCancelled;
  594.     }
  595.     
  596.     ChangedResource ( resourceScratch );
  597.     UpdateResFile ( theFileRef );
  598.     ReleaseResource ( resourceScratch );
  599.     CloseResFile ( theFileRef );
  600.     UseResFile ( saveFile );
  601.     
  602.     SetDocumentDirty ( theWindow, false );
  603.     
  604.     return kNotCancelled;
  605. }
  606.  
  607.  
  608.  
  609. Boolean DoSaveAs ( WindowRef theWindow )
  610. {
  611.     tWindowInfoPtr        theInfo;
  612.     StandardFileReply    theReply;
  613.     Str255                defaultName;
  614.     
  615.     
  616.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  617.     #if DEBUGGING
  618.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  619.     #endif
  620.     
  621.     GetWTitle ( theWindow, defaultName );
  622.     StandardPutFile ( "\p", defaultName, &theReply );
  623.     if ( theReply.sfGood )
  624.     {
  625.         OSErr    theErr;
  626.         
  627.         if ( theInfo->bUntitled )
  628.         {
  629.             theInfo->bUntitled = false;
  630.             DoSave ( theWindow );
  631.             
  632.             theErr = FSpCreate ( &theReply.sfFile, kFourQuestionMarks, kCFragLibraryFileType, theReply.sfScript );
  633.             theErr = FSpExchangeFiles ( &theInfo->fileSpec, &theReply.sfFile );
  634.             FSpDelete ( &theInfo->fileSpec );
  635.             
  636.             BlockMoveData ( &theReply.sfFile, &theInfo->fileSpec, sizeof ( FSSpec ) );
  637.             
  638.             SetWTitle ( theWindow, theReply.sfFile.name );
  639.         }
  640.         else        // User selected “Save As…”
  641.         {
  642.             int16        theRef, saveFile;
  643.             int            i;
  644.             FSSpec        oldSpec;
  645.             tItemPtr    anItem;
  646.             hdrHand        theResource = nil;
  647.             tHeaderHan    theHeader;
  648.             
  649.             
  650.             BlockMoveData ( &theInfo->fileSpec, &oldSpec, sizeof ( FSSpec ) );
  651.             BlockMoveData ( &theReply.sfFile, &theInfo->fileSpec, sizeof ( FSSpec ) );
  652.             theErr = FSpCreate ( &theInfo->fileSpec, kFourQuestionMarks, kCFragLibraryFileType, theReply.sfScript );
  653.             
  654.             theHeader = (tHeaderHan) theInfo->dataHandle;
  655.             HLock ( (Handle) theHeader );
  656.             // copy each fragment's data fork
  657.             for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  658.             {
  659.                 FSSpecPtr    theSpecPtr;
  660.  
  661.                 anItem = GetNthItem ( theHeader, i );
  662.                 if ( anItem->bDeleted )
  663.                     continue;
  664.                 
  665.                 if ( anItem->bExistsInDocument )
  666.                     theSpecPtr = &oldSpec;
  667.                 else
  668.                 {
  669.                     theSpecPtr = GetTempSpecPtr ( anItem );
  670.                     anItem->codeOffset = 0L;
  671.                 }
  672.                     
  673.                 theErr = AppendFileData ( theSpecPtr, &theInfo->fileSpec, &anItem->codeOffset, &anItem->codeLength );
  674.                 if ( theErr )
  675.                 {
  676.                     AlertUser ( kGenericErrorStr, theErr, nil );
  677.                     return kNotCancelled;
  678.                 }
  679.                 
  680.                 anItem->bExistsInDocument = true;
  681.             }
  682.             HUnlock ( (Handle) theHeader );
  683.             
  684.             
  685.             FSpCreateResFile ( &theInfo->fileSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  686.             theRef = FSpOpenResFile ( &theInfo->fileSpec, fsRdWrPerm );
  687.             
  688.             theResource = (hdrHand) NewHandleClear ( offsetof ( cfrgHeader, arrayStart ) );
  689.             (*theResource)->version = 1;        // Current version number
  690.             
  691.             theErr = BuildResource ( (tHeaderHan) theInfo->dataHandle, (Handle) theResource );
  692.             
  693.             saveFile = CurResFile ( );
  694.             theRef = FSpOpenResFile ( &theInfo->fileSpec, fsRdWrPerm );
  695.             // If the file is already open, it may not be the current resource file
  696.             UseResFile ( theRef );
  697.             AddResource ( (Handle) theResource, kCFragResourceType, kCFragResourceID, "\p" );
  698.             UpdateResFile ( theRef );
  699.             ReleaseResource ( (Handle) theResource );
  700.             CloseResFile ( theRef );
  701.             UseResFile ( saveFile );
  702.             
  703.             
  704.             SetWTitle ( theWindow, theReply.sfFile.name );
  705.             
  706.             SetDocumentDirty ( theWindow, false );
  707.         }
  708.         
  709.         return kNotCancelled;
  710.     }
  711.     
  712.     return kCancelled;
  713. }
  714.  
  715.  
  716.  
  717. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon )
  718. {
  719.     OSErr        theErr;
  720.     ListHandle    theList;
  721.     Point        cellSize = {0, 32767};
  722.     Cell        firstCell = {0, 0};
  723.     Rect         dataRect = {0, 0, 0, 1};
  724.     Rect        viewRect;
  725.     
  726.     
  727.     // The width of a cell needs to be 32767 so any hiliting
  728.     // doesn't stop short of the window width after it's been resized.
  729.     
  730.     viewRect = theWindow->portRect;
  731.     viewRect.bottom -= 15; viewRect.right -= 15;
  732.     theList = LNew ( &viewRect, &dataRect, cellSize, 0, theWindow, 
  733.                         false, false, true, true );
  734.     if ( theList )
  735.     {
  736.         (*theList)->selFlags = lOnlyOne;
  737.         (*theList)->lClickLoop = gClickLoopUPP;
  738.         SetWListRef ( theWindow, theList );
  739.         
  740.         // Since the calling routine is always in the same architecture type as
  741.         // the callback routine, we don't need to worry about any Mixed Mode
  742.         // complications. We just treat it as a straight forward routine pointer.
  743.         if ( contentsProc )
  744.         {
  745.             theErr = (*contentsProc) ( theList, refCon );
  746.             if ( theErr )
  747.                 return theErr;
  748.         }
  749.         
  750.         LSetSelect ( true, firstCell, theList );
  751.         
  752.         // Now the list has been fully prepared, turn the drawing mode on
  753.         LSetDrawingMode ( true, theList );
  754.     }
  755.     
  756.     return noErr;
  757.     
  758. } // CreateContentList
  759.  
  760.  
  761.  
  762. Boolean IsDocumentDirty ( WindowRef theWindow )
  763. {
  764.     if ( GetWindowType ( theWindow ) == kDocumentWindowType )
  765.     {
  766.         tWindowInfoPtr    theInfo;
  767.         
  768.         theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  769.         if ( theInfo )
  770.             return ( theInfo->bDirty );
  771.     }
  772.     
  773.     return false;
  774. }
  775.  
  776.  
  777.  
  778. void SetDocumentDirty ( WindowRef theWindow, Boolean bIsDirty )
  779. {
  780.     if ( GetWindowType ( theWindow ) == kDocumentWindowType )
  781.     {
  782.         tWindowInfoPtr    theInfo;
  783.         
  784.         theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  785.         if ( theInfo )
  786.             theInfo->bDirty = bIsDirty;
  787.             
  788.         AdjustMenus ( );
  789.     }
  790.     
  791.     return;
  792. }
  793.  
  794.  
  795.  
  796. void SetWListRef ( WindowRef theWindow, ListHandle theList )
  797. {
  798.     ((tWindowInfoPtr) GetWRefCon ( theWindow ))->listRef = theList;
  799.     
  800.     return;
  801. }
  802.  
  803.  
  804.  
  805. ListHandle GetWListRef ( WindowRef theWindow )
  806. {
  807.     return ((tWindowInfoPtr) GetWRefCon ( theWindow ))->listRef;
  808. }
  809.  
  810.  
  811.  
  812. void SetWFileSpec ( WindowRef theWindow, FSSpecPtr theSpec )
  813. {
  814.     BlockMoveData ( theSpec, &((tWindowInfoPtr) GetWRefCon ( theWindow ))->fileSpec, sizeof ( FSSpec ) );
  815.     
  816.     return;
  817. }
  818.  
  819.  
  820.  
  821. /*----------------------- Handle window update events ----------------------*/
  822.  
  823. void DoUpdate ( EventRecord* theEvent )
  824. {
  825.     WindowRef    theWindow;
  826.     SInt16        theType;
  827.     GrafPtr        savePort;
  828.     
  829.     
  830.     
  831.     theWindow = (WindowRef) theEvent->message;
  832.             
  833.     
  834.     theType = GetWindowType ( theWindow );
  835.     GetPort ( &savePort );
  836.     SetPortWindowPort ( theWindow );
  837.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  838.     EraseRect ( &theWindow->portRect );
  839.  
  840.     switch ( theType )
  841.     {
  842.         case kDocumentWindowType:
  843.         {
  844.             ListHandle    theList = nil;
  845.             
  846.             theList = GetWListRef ( theWindow );
  847.             if ( theList )
  848.                 LUpdate ( (*theList)->port->visRgn, theList );
  849.             DrawClippedGrowIcon ( theWindow );
  850.         }
  851.         break;
  852.         
  853.         case kListWindowType:
  854.         {
  855.             ListHandle        theList = nil;
  856.             tDialogInfoPtr    theInfo;
  857.             
  858.             UpdateDialog ( theWindow, theWindow->visRgn );
  859.             theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  860.             theList = theInfo->listRef;
  861.             if ( theList )
  862.                 LUpdate ( (*theList)->port->visRgn, theList );
  863.         }
  864.         break;
  865.  
  866.         case kGetInfoWindowType:
  867.         {
  868.             TEHandle        textH;
  869.             
  870.             UpdateDialog ( theWindow, theWindow->visRgn );
  871.             textH = ((DialogPeek) theWindow)->textH;
  872.             if ( textH )
  873.                 TEUpdate ( &(*textH)->viewRect, textH );
  874.         }
  875.         break;
  876.     }
  877.     
  878.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  879.     SetPort ( savePort );
  880.     
  881.     return;
  882.     
  883. } // DoUpdate
  884.  
  885.  
  886.  
  887. void DoActivate ( EventRecord* theEvent )
  888. {
  889.     SInt16        itemHit;
  890.     SInt16        theType;
  891.     ListHandle    theList = nil;
  892.     WindowRef    theWindow = (WindowRef) theEvent->message;
  893.     Boolean        bActiveFlag = theEvent->modifiers & 1;
  894.     
  895.     
  896.     
  897.     theType = GetWindowType ( theWindow );
  898.     switch ( theType )
  899.     {
  900.         case kDocumentWindowType:
  901.         {
  902.             LActivate ( bActiveFlag, GetWListRef ( theWindow ) );
  903.             DrawClippedGrowIcon ( theWindow );
  904.         }
  905.         break;
  906.         
  907.         case kListWindowType:
  908.         {
  909.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  910.             theList = theInfo->listRef;
  911.             if ( theList )
  912.                 LActivate ( bActiveFlag, theList );
  913.             DialogSelect ( theEvent, &theWindow, &itemHit );
  914.         }
  915.         break;
  916.         
  917.         case kGetInfoWindowType:
  918.         {
  919.             TEHandle        textH;
  920.             
  921.             textH = ((DialogPeek) theWindow)->textH;
  922.             if ( textH )
  923.             {
  924.                 if ( bActiveFlag )
  925.                     TEActivate ( textH );
  926.                 else
  927.                     TEDeactivate ( textH );
  928.             }
  929.             DialogSelect ( theEvent, &theWindow, &itemHit );
  930.         }
  931.         break;
  932.     }
  933.     
  934.     
  935.     AdjustMenus ( );
  936.     
  937. } // DoActivate
  938.  
  939.  
  940.  
  941. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  942. {
  943.     WindowRef    frontWindow;
  944.     
  945.     // If a movable modal is active, ignore click in an inactive 
  946.     // window, otherwise select it or handle the content click.
  947.     
  948.     frontWindow = FrontWindow ( );
  949.     if ( theWindow != frontWindow )
  950.     {
  951.         if ( IsMovableModal ( frontWindow ) )
  952.             SysBeep ( 30 );
  953.         else
  954.             SelectWindow ( theWindow );
  955.     }
  956.     else
  957.     {
  958.         if ( IsMovableModal ( theWindow ) )
  959.             DoDialogContentClick ( theWindow, theEvent );
  960.         else
  961.         {
  962.             Boolean    bWasHandled;
  963.             bWasHandled = HandleListClick ( theWindow, theEvent );
  964.             
  965.             AdjustMenus ( );
  966.         }
  967.     }
  968.     
  969.     return;
  970.     
  971. } // DoContentClick
  972.  
  973.  
  974.  
  975.  
  976.  
  977. void DoGrowWindow ( WindowRef theWindow, EventRecord* theEvent )
  978. {
  979.     const short kMinDocSize = 64;
  980.     const short kMaxDocSize = 32767;
  981.     
  982.     long    newSize;
  983.     Rect    limitRect;
  984.     
  985.     
  986.     // set up the limiting rectangle
  987.     SetRect ( &limitRect, kMinDocSize, kMinDocSize, kMaxDocSize, kMaxDocSize );
  988.     // call Window Manager to let the user drag size box
  989.     newSize = GrowWindow ( theWindow, theEvent->where, &limitRect );
  990.     if ( newSize )                    // if the user changed its size
  991.     {
  992.         short h, v;
  993.         
  994.         h = newSize & 0x0000FFFF;
  995.         v = newSize >> 16;
  996.         SizeWindow ( theWindow, h, v, true );
  997.         h -= 15; v -= 15;
  998.         LSize ( h, v, GetWListRef ( theWindow ) );
  999.         SetPort ( theWindow );
  1000.         InvalRect ( &theWindow->portRect );
  1001.     }
  1002.     
  1003.     return;
  1004.     
  1005. } // DoGrowWindow
  1006.  
  1007.  
  1008.  
  1009.  
  1010. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  1011. {
  1012.     WindowRef    frontWindow;
  1013.     
  1014.     
  1015.     // If a movable modal is active, ignore click in an inactive 
  1016.     // title bar, otherwise let the Window Manager handle it.
  1017.     
  1018.     frontWindow = FrontWindow ( );
  1019.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  1020.         SysBeep ( 30 );
  1021.     else                                
  1022.     {
  1023.         RgnHandle    theRgn;
  1024.         Rect        dragRect;
  1025.         
  1026.         theRgn = GetGrayRgn ( );
  1027.         dragRect = (*theRgn)->rgnBBox;
  1028.         DragWindow ( theWindow, theEvent->where, &dragRect );
  1029.     }
  1030.     
  1031.     return;
  1032. }
  1033.  
  1034.  
  1035.  
  1036. Boolean IsMovableModal ( WindowRef theWindow )
  1037. {
  1038.     return (GetWVariant ( theWindow ) == movableDBoxProc);
  1039. }
  1040.  
  1041.  
  1042.  
  1043. int16 GetWindowType ( WindowRef theWindow )
  1044. {
  1045.     int16 theType = 0;
  1046.     
  1047.     if ( theWindow )
  1048.     {
  1049.         theType = GetWindowKind ( theWindow );
  1050.         if ( theType < 0 )
  1051.             theType = kDAWindowType;
  1052.         else if ( theType == kApplicationWindowKind )
  1053.             theType = kDocumentWindowType;
  1054.         else
  1055.         {
  1056.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1057.             if ( theInfo )
  1058.                 theType = theInfo->windowType;
  1059.         }
  1060.     }
  1061.     
  1062.     return theType;
  1063. }
  1064.  
  1065.  
  1066.  
  1067. void SetWindowType ( WindowRef theWindow, int16 theType )
  1068. {
  1069.     int16 theKind;
  1070.  
  1071.     if ( theWindow )
  1072.     {
  1073.         theKind = GetWindowKind ( theWindow );
  1074.         if ( theKind != kApplicationWindowKind )
  1075.         {
  1076.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1077.             if ( theInfo )
  1078.                 theInfo->windowType = theType;
  1079.         }
  1080.     }
  1081.     
  1082.     return;
  1083. }
  1084.  
  1085.  
  1086.  
  1087. int16 GetWindowSubType ( WindowRef theWindow )
  1088. {
  1089.     int16 theKind;
  1090.     int16 theType = 0;
  1091.     
  1092.     if ( theWindow )
  1093.     {
  1094.         theKind = GetWindowKind ( theWindow );
  1095.         if ( theKind != kApplicationWindowKind )
  1096.         {
  1097.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1098.             if ( theInfo )
  1099.                 theType = theInfo->windowSubType;
  1100.         }
  1101.     }
  1102.     
  1103.     return theType;
  1104. }
  1105.  
  1106.  
  1107.  
  1108. void SetWindowSubType ( WindowRef theWindow, int16 theType )
  1109. {
  1110.     int16 theKind;
  1111.  
  1112.     if ( theWindow )
  1113.     {
  1114.         theKind = GetWindowKind ( theWindow );
  1115.         if ( theKind != kApplicationWindowKind )
  1116.         {
  1117.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1118.             if ( theInfo )
  1119.                 theInfo->windowSubType = theType;
  1120.         }
  1121.     }
  1122.     
  1123.     return;
  1124. }
  1125.  
  1126.  
  1127.  
  1128. void DoZoomWindow ( WindowRef theWindow, EventRecord* theEvent, int16 windowPart )
  1129. {
  1130.     Point                globalPt = theEvent->where;
  1131.     
  1132.     SetPort ( theWindow );
  1133.     if ( TrackBox ( theWindow, globalPt, windowPart ) )
  1134.     {
  1135.         /*
  1136.             TO DO:
  1137.                 This doesn't handle multiple screens
  1138.         */
  1139.         
  1140.         ZoomWindow ( theWindow, windowPart, true );
  1141.         InvalRect ( &theWindow->portRect );
  1142.     }
  1143.     
  1144.     return;
  1145. }
  1146.  
  1147.  
  1148.  
  1149. OSErr CopyWindowFragment ( WindowRef source, int16 sourceIndex, WindowRef target )
  1150. {
  1151.     int16            itemIndex;
  1152.     OSErr            theErr;
  1153.     tWindowInfoPtr    sourceInfo = nil;
  1154.     tWindowInfoPtr    targetInfo = nil;
  1155.     
  1156.     
  1157.     
  1158.     itemIndex = GetIndexFromNthWindowItem ( source, sourceIndex );
  1159.     
  1160.     sourceInfo = (tWindowInfoPtr) GetWRefCon ( source );
  1161.     targetInfo = (tWindowInfoPtr) GetWRefCon ( target );
  1162.     
  1163.     theErr = CreateTempAndCopyFragment ( (tHeaderHan) sourceInfo->dataHandle, &sourceInfo->fileSpec,
  1164.                                             itemIndex, (tHeaderHan) targetInfo->dataHandle );
  1165.     if ( theErr == noErr )
  1166.     {
  1167.         SignedByte    theState;
  1168.         tItemPtr    theItem;
  1169.         
  1170.         theState = HGetState ( sourceInfo->dataHandle );
  1171.         HLock ( sourceInfo->dataHandle );
  1172.         theItem = GetNthItem ( (tHeaderHan) sourceInfo->dataHandle, itemIndex );
  1173.         AddFragToList ( GetWListRef ( target ), theItem );
  1174.         HSetState ( sourceInfo->dataHandle, theState );
  1175.         
  1176.         SetDocumentDirty ( target, true );
  1177.     }
  1178.     
  1179.     return theErr;
  1180. }
  1181.  
  1182.  
  1183.  
  1184. OSErr MoveWindowFragment ( WindowRef source, int16 sourceIndex, WindowRef target )
  1185. {
  1186.     OSErr    theErr = noErr;
  1187.     
  1188.     theErr = CopyWindowFragment ( source, sourceIndex, target );
  1189.     if ( !theErr )
  1190.         theErr = DeleteWindowFragment ( source, sourceIndex );
  1191.     
  1192.     return theErr;
  1193. }
  1194.  
  1195.  
  1196.  
  1197. OSErr DeleteWindowFragment ( WindowRef theWindow, int16 theIndex )
  1198. {
  1199.     int16            itemIndex;
  1200.     OSErr            theErr;
  1201.     tWindowInfoPtr    theInfo;
  1202.     
  1203.     
  1204.     // Since we don't support undo just yet, there's no need
  1205.     // to copy it to a temporary file.
  1206.     
  1207.     itemIndex = GetIndexFromNthWindowItem ( theWindow, theIndex );
  1208.     
  1209.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  1210.     theErr = DeleteFragment ( (tHeaderHan) theInfo->dataHandle, nil, itemIndex );
  1211.     
  1212.     if ( theErr == noErr )
  1213.     {
  1214.         DeleteFromList ( GetWListRef ( theWindow ), theIndex );
  1215.         SetDocumentDirty ( theWindow, true );
  1216.     }
  1217.     
  1218.     return theErr;
  1219. }
  1220.  
  1221.  
  1222.  
  1223. OSErr CreateTempAndCopyFragment ( tHeaderHan sourceHeader, FSSpecPtr sourceSpec, int16 sourceIndex,
  1224.                                         tHeaderHan targetHeader )
  1225. {
  1226.     SignedByte    sourceState, targetState;
  1227.     OSErr        theErr;
  1228.     tItemPtr    sourceItem, targetItem;
  1229.     FSSpec        tempSpec;
  1230.     
  1231.     
  1232.     sourceState = HGetState ( (Handle) sourceHeader );
  1233.     HLock ( (Handle) sourceHeader );
  1234.     targetState = HGetState ( (Handle) targetHeader );
  1235.     HLock ( (Handle) targetHeader );
  1236.     
  1237.     sourceItem = GetNthItem ( sourceHeader, sourceIndex );
  1238.     // Do we already have a temporary file that contains this fragment
  1239.     if ( GetTempUsageCount ( sourceItem ) )
  1240.     {
  1241.         theErr = CopyFragment ( sourceHeader, nil, sourceIndex, targetHeader, nil );
  1242.         
  1243.         targetItem = GetLastItem ( targetHeader );
  1244.         targetItem->bExistsInDocument = false;
  1245.         IncrementTempUsageCount ( targetItem );
  1246.     }
  1247.     else
  1248.     {
  1249.         theErr = CreateTemporaryFile ( &tempSpec );
  1250.         
  1251.         theErr = CopyFragment ( sourceHeader, sourceSpec, sourceIndex, targetHeader, &tempSpec );
  1252.         if ( theErr )
  1253.             // We'll let the system handle the temp file. It may
  1254.             // be useful for debugging, if nothing else.
  1255.             return theErr;
  1256.         
  1257.         sourceItem = GetNthItem ( sourceHeader, sourceIndex );
  1258.         IncrementTempUsageCount ( sourceItem );
  1259.         BlockMoveData ( &tempSpec, GetTempSpecPtr ( sourceItem ), sizeof ( FSSpec ) );
  1260.         
  1261.         targetItem = GetLastItem ( targetHeader );
  1262.         targetItem->bExistsInDocument = false;
  1263.         targetItem->tempFilePtr = sourceItem->tempFilePtr;
  1264.         IncrementTempUsageCount ( targetItem );
  1265.     }
  1266.     
  1267.     HSetState ( (Handle) sourceHeader, sourceState );
  1268.     HSetState ( (Handle) sourceHeader, targetState );
  1269.     
  1270.     return theErr;
  1271. }
  1272.  
  1273.  
  1274.  
  1275. tItemPtr GetNthWindowItem ( WindowRef theWindow, int16 theIndex )
  1276. {
  1277.     tWindowInfoPtr    theInfo = nil;
  1278.     
  1279.     
  1280.     theIndex = GetIndexFromNthWindowItem ( theWindow, theIndex );
  1281.     
  1282.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  1283.     return GetNthItem ( (tHeaderHan) theInfo->dataHandle, theIndex );
  1284. }
  1285.  
  1286.  
  1287.  
  1288. int16 GetIndexFromNthWindowItem ( WindowRef theWindow, int16 theIndex )
  1289. {
  1290.     int16            theSize;
  1291.     int                i;
  1292.     Cell            theCell = { 0, 0 };
  1293.     tWindowInfoPtr    theInfo = nil;
  1294.     tHeaderHan        theHeader;
  1295.     Str255            theText;
  1296.     
  1297.     
  1298.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  1299.     
  1300.     // First, get the text we're looking for. Two fragments with the same name
  1301.     // but different architectures are quite likely. We need to include the type.
  1302.     
  1303.     theSize = sizeof ( Str255 );
  1304.     theCell.v = theIndex;
  1305.     LGetCell ( &theText[1], &theSize, theCell, theInfo->listRef );
  1306.     theText[0] = theSize;
  1307.     
  1308.     theHeader = (tHeaderHan) theInfo->dataHandle;
  1309.     // Now, scan our items looking for a match
  1310.     for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  1311.     {
  1312.         tItemPtr        theItem;
  1313.         unsigned char    archTypeStr[5];
  1314.         Str255            itemText;
  1315.         
  1316.         theItem = GetNthItem ( theHeader, i );
  1317.         // Since we're looking for an item in the window, ignore deleted items
  1318.         if ( theItem->bDeleted )
  1319.             continue;
  1320.         
  1321.         CopyPStr ( theItem->name, itemText, sizeof ( Str255 ) );
  1322.         ConcatPStr ( itemText, "\p\t", sizeof ( Str255 ) );
  1323.         OSTypeToPStr ( theItem->archType, archTypeStr );
  1324.         ConcatPStr ( itemText, archTypeStr, sizeof ( Str255 ) );
  1325.         if ( EqualString ( theText, itemText, true, true ) )
  1326.             return i;
  1327.     }
  1328.     
  1329.     
  1330.     #if DEBUGGING
  1331.     DebugStr ( "\p GetIndexFromNthWindowItem returning 0" );
  1332.     #endif
  1333.  
  1334.     return 0;
  1335. }
  1336.  
  1337.  
  1338.  
  1339. void AddFragToList ( ListRef theList, tItemPtr theItem )
  1340. {
  1341.     unsigned char    archTypeStr[5];
  1342.     Str255            theText;
  1343.     
  1344.     
  1345.     CopyPStr ( theItem->name, theText, sizeof ( Str255 ) );
  1346.     ConcatPStr ( theText, "\p\t", sizeof ( Str255 ) );
  1347.     OSTypeToPStr ( theItem->archType, archTypeStr );
  1348.     ConcatPStr ( theText, archTypeStr, sizeof ( Str255 ) );
  1349.     AddToList ( theList, theText );
  1350.     
  1351.     return;
  1352. }
  1353.  
  1354.  
  1355.  
  1356. void UpdateFragInList ( WindowRef theWindow, int16 theIndex, StringPtr newName )
  1357. {
  1358.     unsigned char    archTypeStr[5];
  1359.     tItemPtr        theItem;
  1360.     ListRef            theList;
  1361.     Str255            theText;
  1362.     
  1363.     
  1364.     theItem = GetNthWindowItem ( theWindow, theIndex );
  1365.     theList = GetWListRef ( theWindow );
  1366.     CopyPStr ( newName, theText, sizeof ( Str255 ) );
  1367.     ConcatPStr ( theText, "\p\t", sizeof ( Str255 ) );
  1368.     OSTypeToPStr ( theItem->archType, archTypeStr );
  1369.     ConcatPStr ( theText, archTypeStr, sizeof ( Str255 ) );
  1370.     UpdateList ( theList, theIndex, theText );
  1371.     
  1372.     return;
  1373. }
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.